package org.example.backend.demos.web.UserModule.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * 登录结果
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class LoginResult implements Serializable, UserDetails {
    private static final long serialVersionUID = -11709912532294169L;

    /**
     * 用户ID
     */
    private Long id;

    /**
     * 用户名
     */
    private String username;



    private String password;

    /**
     * 角色
     */
    private String role;

    /**
     * JWT Token
     */
    private String token;



    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (role != null) {
            return List.of(new SimpleGrantedAuthority("ROLE_" + role));
        }
        return List.of();
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public boolean isAccountNonExpired() {
        // 根据业务逻辑判断账户是否过期
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        // 根据业务逻辑判断账户是否被锁定
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        // 根据业务逻辑判断密码是否过期
        return true;
    }

    @Override
    public boolean isEnabled() {
        // 根据业务逻辑判断账户是否启用
        return true;
    }

}